# 单纯的重复，不是迭代
"""
i = 0
while True:
    print(i)
"""
# 迭代是一重复的过程，每一次重复都是基于上一次的结果而来
"""
l = ['a','b','c']
i = 0
while i< len(l):
    print(i)
    print(l[i])
    i+=1
"""
# 元组、列表、集合、字符串都可以迭代
"""
t = ('a','b','c')
for i in t:
    print(i)
"""

# 可迭代对象 iter 和next也被称为迭代器协议
a = 1
# a.__iter__ 没有
b = 1.1
# b.__iter__ 没有
c = "hello" # 有__iter__方法
"""
print(c.__iter__()) # 有
c_iter = c.__iter__() # 创建了一个迭代器
print(c_iter.__next__()) # 访问下一个元素
print(c_iter.__next__()) # 访问下一个元素
print(c_iter.__next__()) # 访问下一个元素
print(c_iter.__next__()) # 访问下一个元素
print(c_iter.__next__()) # 访问下一个元素
print(c_iter.__next__()) # 访问下一个元素 StopIteration：取没了
"""
d = ['a','b']
d.__iter__() # 有
g = {1,2}
g.__iter__() # 有
e = (1,2)
e.__iter__() # 有
"""
l = [1,2,3]
l1 = l.__iter__()
print(l1.__next__())
print(l1.__next__())
print(l.__iter__().__next__())
print(l.__iter__().__next__())
print(l.__iter__().__next__())
print(l.__iter__().__next__())
"""
# 生成器
def g():
    a = 1
    while True:
        yield a # yield 当成return
        a +=1
a1 = g()
print(next(a1)) #
print(next(a1))
print(next(a1))





